package org.example.aitest.utils.gen;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DynamicDataService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 动态插入数据
    public void insertData(String tableName, Map<String, Object> data) {
        // 构建动态SQL
        String columns = String.join(", ", data.keySet());
        String values = data.values().stream()
            .map(v -> v instanceof String ? "'" + v + "'" : v.toString())
            .collect(Collectors.joining(", "));

        String sql = "INSERT INTO %s (%s) VALUES (%s)".formatted(tableName, columns, values);
        System.out.println(sql);
        jdbcTemplate.execute(sql);
    }
}